package com.kaifamiao.dispatch;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/included")
public class IncludedServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.log("Included");

        response.setCharacterEncoding( "UTF-8" );
        response.setContentType( "text/html;charset=UTF-8" );
        PrintWriter out = response.getWriter();

        out.println( "<style>");
        out.println( "div { border: 1px solid blue; margin: 15px; }");
        out.println( "</style>");

        out.println( "<div>included servlet</div>" );

        out.println( "<div>" );
        // 指定被包含资源的路径
        String path1 = "/inner";
        // 获得用于包含指定资源的请求派遣器
        RequestDispatcher dispatcher1 = request.getRequestDispatcher( path1 );
        // 包含指定资源
        dispatcher1.include(request, response);
        out.println( "</div>" );

        out.println( "<div>" );
        String path2 = "/WEB-INF/inner.html";
        RequestDispatcher dispatcher2 = request.getRequestDispatcher( path2 );
        dispatcher2.include(request, response);
        out.println( "</div>" );

        out.println( "<div>" );
        String path3 = "/WEB-INF/inner.txt";
        RequestDispatcher dispatcher3 = request.getRequestDispatcher( path3 );
        dispatcher3.include(request, response);
        out.println( "</div>" );

    }

}
